package com.wl.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.注销把Session清空
        HttpSession session = req.getSession(false);
        if (session != null){
            session.setMaxInactiveInterval(0);
            // 销毁session
            session.invalidate();
        }

        // 2.cookie清空
        Cookie[] cookies = req.getCookies();
        if(cookies != null){
            for(Cookie c : cookies){
                c.setMaxAge(0);
                resp.addCookie(c);
            }
        }
        // 重定向
        resp.sendRedirect("/index.html");
    }
}
